<template>
  <div class="execution">
    <basic-container>
      <el-form ref="dataForm" :model="dataForm" :rules="rules" label-width="150px" size="small">
        <el-form-item label="小程序AppId" prop="appId">
          <el-input v-model="dataForm.appId" style="width: 400px" placeholder="请填写小程序AppId"></el-input>
        </el-form-item>
        <el-form-item label="小程序appSecret" prop="appSecret">
          <el-input v-model="dataForm.appSecret" style="width: 400px" placeholder="请填写小程序密钥">
          </el-input>
        </el-form-item>
        <el-form-item label="公众号AppId" prop="appId">
          <el-input v-model="dataForm.pubAppId" style="width: 400px" placeholder="请填写小程序AppId"></el-input>
        </el-form-item>
        <el-form-item label="公众号appSecret" prop="appSecret">
          <el-input v-model="dataForm.pubAppSecret" style="width: 400px" placeholder="请填写小程序密钥">
          </el-input>
        </el-form-item>
        <el-form-item label="小程序商户号" prop="mchId">
          <el-input v-model="dataForm.mchId" style="width: 400px" placeholder="请填写小程序商户号">
          </el-input>
        </el-form-item>
        <el-form-item label="小程序支付密钥" prop="paySecret">
          <el-input v-model="dataForm.paySecret" style="width: 400px" placeholder="请填写小程序支付密钥">
          </el-input>
        </el-form-item>
        <el-form-item label="微信回调地址" prop="notifyUrl">
          <el-input v-model="dataForm.notifyUrl" style="width: 400px" placeholder="请填写微信回调地址">
          </el-input>
        </el-form-item>
        <el-form-item label="小程序转发主图">
          <el-upload
            :action="actionUrl"
            :headers="headers"
            :limit="1"
            :file-list="fileList"
            list-type="picture-card"
            :on-success="handleUploadSuccess"
            :on-remove="handleImageRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" :loading="loading">保存</el-button>
          <el-button @click="getList()">取消</el-button>
        </el-form-item>
      </el-form>
    </basic-container>
  </div>
</template>

<script>
  import Vue from 'vue'
  import {getConfig, saveConfig, saveIndexInfoConfig} from '@/api/admin/config'
  import {handleImgUrl} from '@/utils/util'

  export default {
    name: 'config',
    data () {
      return {
        dataForm: {},
        fileList: [],
        loading: false,
        actionUrl: '',
        saveMessageTem: '',
        headers: {
          'token': Vue.cookie.get('token')
        },
        rules: {
          appId: [
            {required: true, message: '请填写小程序AppId', trigger: 'blur'}
          ],
          pubAppId: [
            {required: true, message: '请填写公众号AppId', trigger: 'blur'}
          ],
          mchId: [
            {required: true, message: '请填写小程序商户号', trigger: 'blur'}
          ],
          appSecret: [
            {required: true, message: '请填写小程序密钥', trigger: 'blur'}
          ],
          pubAppSecret: [
            {required: true, message: '请填写小程序密钥', trigger: 'blur'}
          ],
          notifyUrl: [
            {required: true, message: '请填写微信回调地址', trigger: 'blur'}
          ],
          paySecret: [
            {required: true, message: '请填写小程序支付密钥', trigger: 'blur'}
          ]
        }
      }
    },
    mounted: function () {
      this.actionUrl = this.$http.adornUploadUrl()
      this.getList()
    },
    methods: {
      getList () {
        getConfig().then(response => {
          this.dataForm = response.data.data
          if (this.dataForm.sgShareImage) {
            handleImgUrl(this.dataForm.sgShareImage).then((res) => {
              this.fileList = [{
                url: res.data
              }]
            })
          }
        })
      },
      /**
       * @title 数据添加
       * @param row 为当前的数据
       * @param done 为表单关闭函数
       *
       **/

      onSubmit () {
        this.loading = true
        const data = new FormData()
        data.append('paramValue', JSON.stringify(this.dataForm))
        saveConfig(data).then(res => {
          this.loading = false
          this.$message({
            showClose: true,
            message: '保存成功',
            type: 'success'
          })
          this.getList()
        })
      },
      handleUploadSuccess (res, file) {
        this.dataForm.sgShareImage = res.data.bucketName + '-' + res.data.fileName
      },
      handleImageRemove () {
        this.fileList = []
        this.dataForm.sgShareImage = ''
      },
//      /**
//       * 刷新回调
//       */
//      refreshChange() {
//        this.getList(this.page)
//      }
    }
  }
</script>

<style lang="scss" scoped>
</style>
